package orm;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MyBatisSQLSession {
    private static SqlSession sqlSession;

    public static void init() {
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader("Mybatis_SQLite_Config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSession = new SqlSessionFactoryBuilder().build(reader).openSession(true);
    }

    public static SqlSession getSqlSession() {
        if (sqlSession == null) {
            init();
        }
        return sqlSession;
    }

    public static WordMapper getWordMapper() {
        return getSqlSession().getMapper(WordMapper.class);
    }

    public static UserMapper getUserMapper() {
        return getSqlSession().getMapper(UserMapper.class);
    }
}
